<?php
namespace App\Repos;

use App\Models\Task;
use App\Models\User;
use App\Models\TaskProcess;
use App\Models\Depart;
use App\Models\Worktype;
use DB,Log;
use App\Services\Wechat;

class TaskRepo
{
    // 工单完成
    public static function complete($data)
    {
        $rs['status'] = true;
        try {
            DB::beginTransaction();
            $task = Task::where('id',$data['task_id'])->first();
            $task->status = 100;
            $task->save();
            $depart = Depart::where(['id'=>$task->depart_id])->first();
            $process = TaskProcess::select(['task_id','worker_id','worker_openid'])
                                    ->where(['task_id'=>$data['task_id']])
                                    ->first();
            $work_pic = rtrim(array_get($data,'pics'),',');                       
            if($process) {
                $proData = $process->toArray();                
                $proData['work_pic'] = $work_pic;
                TaskProcess::create($proData);
            } else {
                $proData['task_id']   = $task->id;
                $proData['worker_id'] = $depart->worker_id;
                $proData['remark']    = $data['remark'];
                $proData['work_pic']  = $work_pic;
                $proData['status'] = 100;
                TaskProcess::create($proData);
            }
            
        } catch (\Exception $e) {
           $rs['status'] = false;
           $rs['msg'] = $e->getMessage();
           DB::rollBack(); 
        }
        DB::commit();

        // 推送给用户
        $msg = $task->toArray();
        $msg['first']      = '信息处理成功通知';
        $msg['status_txt'] = '处理成功';
        $msg['result']     = '已经对商户进行处理，并且随时跟进中…';
        $msg['url']        = config('app.url').'user/task/show/'.$task->id;
        $msg['remark'] = '点击查看详情!';
        Wechat::sendMsgTpl('process',$msg);

        // 推送给监管
        
            
        
        if(object_get($depart,'manager','openid')) {
                $msg['openid'] = object_get($depart,'manager.openid');
                Wechat::sendMsgTpl('process',$msg);
        }
        if(object_get($depart,'worker','openid')) {
            $msg['openid'] = object_get($depart,'worker.openid');
            Wechat::sendMsgTpl('process',$msg);
        }
       
        

        return $rs;

    }
	
    public static function sendNewMsg($data)
    {
        // 先给提交的用户回执
        $msg = $data;
        $msg['first']      = '信息受理成功通知';
        $msg['status_txt'] = '受理成功';
        if($data['category_id']==1) {
            $msg['result']     = '等待处理结果';
        } else {
            $msg['result']     = '已经成功推送信息给相关负责人';
        }
        
        $msg['url']    = config('app.url').'user/task/show/'.$data['id'];
        $msg['remark'] = '';
        Wechat::sendMsgTpl('process',$msg);
     
        
        
        // 如果是商场监管 推送给商场相关人
        if($data['category_id']==1) {
            $msg['first'] = '商场监管受理通知';

            $depart = Depart::where(['id'=>$data['depart_id']])->first();
            $msg['shenfen'] = $depart->name.'（消费者）';
            $msg['customer_name'] = $data['truename'].'（'.$data['mobile'].'）';
            $msg['remark'] = '点击查看详情并处理!';
            if(object_get($depart,'manager','openid')) {
                $msg['openid'] = object_get($depart,'manager.openid');
                Wechat::sendMsgTpl('jianguan',$msg);
            }
            if(object_get($depart,'worker','openid')) {
                    $msg['openid'] = object_get($depart,'worker.openid');
                    Wechat::sendMsgTpl('jianguan',$msg);
            }           
        } else {  
         
        // 加盟合作微信推送
            $msg['first'] = '加盟合作受理通知';
           
            $worktype = Worktype::where(['name'=>$data['shenfen']])->first();
            Log::info(object_get($worktype,'worker','openid'));    
            if(object_get($worktype,'worker','openid')) {
                $msg['openid']        = $worktype->worker->openid;
                $msg['shenfen']       = $data['shenfen'];
                $msg['customer_name'] = $data['truename'].'（'.$data['mobile'].'）';
                $msg['remark'] = '点击查看详情并处理';
                Wechat::sendMsgTpl('jianguan',$msg);
            }
        }
    }

    /**
     * 推送完工模板
     */
    public static function sendCompleteMsg($data)
    {
        // 推送消息给提交者     

        Wechat::sendMsgTpl('complete',$data);  // 先推送给客户
        
        $msg = $data;
        $depart = Depart::where(['id'=>$data['depart_id']])->first();
        if(object_get($depart,'manager','openid')) {
            $msg['openid'] = object_get($depart,'manager','openid');
            Wechat::sendMsgTpl('complete',$msg);
        } 
    }
	

    


    

	
	
}